
var app = getApp();
const $ = require('../../utils/utils');
const config = require('../../config');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        footerConfig: {
            loadingText: "正在努力的加载数据...",
        },
        imgurl: '',
        book_count: 0,
        user_name: '',
        rank: 0,
        rankList: [],
        shareShow: 0,
        groupId: '',
        shareImg: '', 
        content:''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        
        console.log('options', options)

        var that = this
        if (wx.getStorageSync('toShareGroup')) {
            this.setData({
                shareShow: 1,
                path: options.path
            })
            this.setData({
                rankList: [1, 2]
            })

            wx.showShareMenu({
                withShareTicket: true
            })
            wx.setStorageSync('toShareGroup', false)
        }else{
            wx.hideShareMenu()
        }


        console.log('rank-----list',wx.getStorageSync("isLogin"))
        this.getRankData()
       
       
        // if (wx.getStorageSync("isLogin")) {
        //     this.getRankData()
        // } else {
        //     if(wx.getStorageSync('usertoken')){
        //         this.getRankData()
        //     }else{
        //         app.addListener(function (changedData) {
        //             console.log("#index_addListener()", wx.getStorageSync("isLogin"))
        //             that.getRankData()
        //         });
        //     }
        // }


    },


    /**
    * 生命周期函数--监听页面显示
    */
    onShow: function () {
        let that = this
        
    },


    shareToGroupChat: function () {
        wx.showShareMenu({
            withShareTicket: true
        })
    },


    gotoWeekList: function () {
        $.goto(config.page.weekRankList, true)
    },

    getRankData:function(){
        if (wx.getStorageSync("shareTicket")) {
            console.log('#groupRanklist_getRankData()__shareTicket', wx.getStorageSync("shareTicket"))
            this.getOpenGid()
        } else {
            console.log('#groupRanklist_getRankData()__NO___shareTicket')
            $.goto(config.page.weekRankList)
        }
    },
    

    // 获取群id，并获取群排行
    getOpenGid:function(){

        let that = this 

        wx.getShareInfo({
            shareTicket: wx.getStorageSync("shareTicket"),
            success: function (res) {
                console.log('success')
                var encryptedData = res.encryptedData;
                var iv = res.iv;
                let data = {
                    encryptedData: encryptedData,
                    iv: iv,
                    mobile: wx.getStorageSync("isLogin") ? wx.getStorageSync('userPhone'):'',
                    userid: wx.getStorageSync("isLogin") ? wx.getStorageSync('userid') : '',
                    usertoken: wx.getStorageSync('usertoken'),
                    activity_id: 'activity_id'
                }
                console.log(data)
                let url = config.queryGroupList
                $.post(url, data).then(data => {
                    console.log('group=-------', data)
                    that.setData({
                        groupId: JSON.parse(data.detail.decryptInfo).openGId
                    })

                    that.queryPersonInWeekRankActivity(that.data.groupId)
                })
            },
            fail: function (err) {
                console.log('err', err)
            }
        })    
    },


    queryPersonInWeekRankActivity: function (groupId) {

        let data = {
            groupId: groupId,
            userid: wx.getStorageSync("isLogin") ? wx.getStorageSync('userid'):'',
            usertoken: wx.getStorageSync('usertoken'),
            activity_id: 'activity_id'
        }
        console.log('queryPersonInWeekRankActivityData', data)
        let url = $.getUrl(config.queryPersonInWeekRankActivity, data)
        $.get(url).then(data => {
            console.log('queryPersonInWeekRankActivity------------', data)
            this.setData({
                imgurl: data.detail.imgurl,
                book_count: data.detail.book_count,
                user_name: data.detail.user_name,
                rankList: data.items,
                rank: data.detail.rank,
                content: data.content,
            })
        })
    },


    /***
     * 周排行分享出去=》群排行
     * 点击分享卡片=》if（opengid）判断登陆=》if(!denglu)=>登陆else if(！opengid)=>周榜排行
     * ****/

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function (res) {
        let that = this
        that.setData({
            rankList: []
        })
        return {
            title: '谁将会是本周的最佳阅读宝宝',
            path: config.page.groupRankList,
            imageUrl: "http://wdb007.oss-cn-hangzhou.aliyuncs.com/wxapp/images/group_rank_share.png",
            success: function (res) {
                wx.getShareInfo({
                    shareTicket: res.shareTickets[0],
                    success: function (res) {
                        console.log('success')
                        var encryptedData = res.encryptedData;
                        var iv = res.iv;
                        let data = {
                            unionid: wx.getStorageSync('unionid'),
                            encryptedData: encryptedData,
                            iv: iv,
                            sessionKey: wx.getStorageSync('sessionKey'),
                            activity_id: 'activity_id'
                        }
                        console.log(data)
                        let url = config.queryGroupList
                        $.post(url, data).then(data => {
                            console.log('group=-------', data)
                        })
                        wx.removeStorageSync("toShareGroup")
                    },
                    fail: function (err) {
                        console.log('err', err)
                    }
                })
            },
            fail: function (res) {
                // 转发失败
            }
        }
    }
})